package com.infomanage.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.infomanage.bean.FieldBean;
import com.infomanage.bean.SearchBean;
import com.infomanage.exception.DaoException;
import com.infomanage.po.Department;
import com.infomanage.po.Dutylog;
import com.infomanage.po.Dutyperson;
import com.infomanage.po.HibernateSessionFactory;
import com.infomanage.po.Person;
import com.infomanage.vo.DutylogItem;
import com.infomanage.vo.DutypersonItem;

public class DutypersonDAO extends BaseDAO<Dutyperson>{
	public DutypersonItem find(int id) throws DaoException{
		try {
			SearchBean searchBean  = new SearchBean();
			FieldBean fieldBean  = new FieldBean();
			fieldBean.setName("dutypersonId");
			fieldBean.setValue(id);
			searchBean.add(fieldBean);
			List result = super.findByFields(new Class[] { Person.class, Person.class , Person.class, Person.class},
					Dutyperson.class, new String[] {"personId", "personId", "personId", "personId" },
					new String[] {"masterPersonId", "slavePersonId", "attendantPersonId", "dispatcherPersonId"}, searchBean);
			List<DutypersonItem> items = parseResult(result);
			return items.size() == 0 ? null : items.get(0);
		} catch (Exception e) {
			throw new DaoException(e);
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}
	@SuppressWarnings("unchecked")
	public List<DutypersonItem> findAll() throws DaoException {
		try {
			List result = findAllMutilTable(new Class[] { Person.class, Person.class, Person.class, Person.class },
					Dutyperson.class, new String[] {"personId", "personId", "personId", "personId" },
					new String[] {"masterPersonId", "slavePersonId", "attendantPersonId", "dispatcherPersonId"});
			return parseResult(result);
		} catch (Exception e) {
			throw new DaoException(e);
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<DutypersonItem> findByFields(SearchBean searchBean) throws DaoException {
		List result = super.findByFields(new Class[] { Person.class, Person.class , Person.class, Person.class},
				Dutyperson.class, new String[] {"personId", "personId", "personId", "personId" },
				new String[] {"masterPersonId", "slavePersonId", "attendantPersonId", "dispatcherPersonId"}, searchBean);
		return parseResult(result);
	}
	
	public List<DutypersonItem> parseResult(List result){
		Iterator it = result.iterator();
		List<DutypersonItem> list = new ArrayList<DutypersonItem>();
		while (it.hasNext()) {
			Object[] o = (Object[]) it.next();
			Dutyperson log = (Dutyperson) o[0];
			Person masterPerson = (Person) o[1];
			Person slavePerson = (Person) o[2];
			Person attendantPerson = (Person) o[3];
			Person dispatcherPerson = (Person) o[4];
			DutypersonItem item = new DutypersonItem();
			item.setBean(log);
			item.setMasterPerson(masterPerson);
			item.setSlavePerson(slavePerson);
			item.setAttendantPerson(attendantPerson);
			item.setDispatcherPerson(dispatcherPerson);
			list.add(item);
		}
		return list;
	}
	
	public int count(SearchBean searchBean){
		return super.count(new Class[] { Person.class, Person.class, Person.class, Person.class },
				Dutyperson.class, new String[] {"personId", "personId", "personId", "personId" },
				new String[] {"masterPersonId", "slavePersonId", "attendantPersonId", "dispatcherPersonId"}, searchBean);
	}
}
